home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / msysjour / vol05 / 05 / forms / viewer.c < prev    next >
Text File  |  1990-09-01  |  14KB  |  555 lines

  1. /*
  2.  * FORM VIEWER - SOURCE CODE
  3.  *
  4.  * LANGUAGE      : Microsoft C 5.1
  5.  * MODEL         : medium
  6.  * ENVIRONMENT   : Microsoft Windows 3.00 SDK
  7.  * STATUS        : operational
  8.  *
  9.  * 07/12/90 1.00 - Kevin P. Welch - initial creation.
  10.  *
  11.  */
  12.  
  13. #define NOCOMM
  14.  
  15. #include <windows.h>
  16. #include <memory.h>
  17. #include "viewer.h"
  18.  
  19. /**/
  20.  
  21. /*
  22.  * WinMain( hCrntInst, hPrevInst, lpszCmd, wCmdShow ) : VOID
  23.  *
  24.  *    hCrntInst      current instance handle
  25.  *    hPrevInst      previous instance handle
  26.  *    lpszCmd        current command line
  27.  *    wCmdShow       initial show window parameter
  28.  *
  29.  * This function is responsible for registering and creating the form
  30.  * viewer window.  Once the window has been created, messages
  31.  * are retrieved until the window is destroyed.
  32.  *
  33.  */
  34.  
  35. int PASCAL WinMain(
  36.    HANDLE      hCrntInst,
  37.    HANDLE      hPrevInst,
  38.    LPSTR       lpszCmd,
  39.    WORD        wCmdShow )
  40. {
  41.    /* local variables */
  42.    MSG         Msg;
  43.    HWND        hWnd;
  44.    WNDCLASS    WndClass;
  45.  
  46.    /* warning level 3 compatibility */
  47.    lpszCmd;
  48.  
  49.    /* define special window class */
  50.    memset( &WndClass, 0, sizeof(WNDCLASS) );
  51.  
  52.    WndClass.lpszClassName =   "Viewer";
  53.    WndClass.hInstance =       hCrntInst;
  54.    WndClass.lpfnWndProc =     ViewerWndFn;
  55.    WndClass.lpszMenuName =    "ViewerMenu";
  56.    WndClass.hbrBackground =   NULL;
  57.    WndClass.style =           CS_HREDRAW | CS_VREDRAW;
  58.    WndClass.hCursor =         LoadCursor( NULL, IDC_ARROW );
  59.    WndClass.hIcon =           LoadIcon( hCrntInst, "ViewerIcon" );
  60.  
  61.    if ( (hPrevInst) || (RegisterClass(&WndClass)) ) {
  62.  
  63.       /* create main window */
  64.       hWnd = CreateWindow(
  65.             "Viewer",
  66.             "Form Viewer - (untitled)",
  67.             VIEWER_STYLE,
  68.             VIEWER_XPOS,
  69.             VIEWER_YPOS,
  70.             VIEWER_WIDTH,
  71.             VIEWER_HEIGHT,
  72.             (HWND)NULL,
  73.             (HMENU)NULL,
  74.             hCrntInst,
  75.             (LPSTR)&WndClass
  76.          );
  77.  
  78.       /* continue if successful */
  79.       if ( hWnd ) {
  80.  
  81.          /* display window */
  82.          ShowWindow( hWnd, wCmdShow );
  83.  
  84.          /* process messages */
  85.          while ( GetMessage(&Msg,NULL,0,0) ) {
  86.             TranslateMessage( &Msg );
  87.             DispatchMessage( &Msg );
  88.          }
  89.  
  90.          /* normal exit */
  91.          return( Msg.wParam );
  92.  
  93.       } else
  94.          WARNING( NULL, "Unable to create window!" );
  95.  
  96.    } else
  97.       WARNING( NULL, "Unable to register window!" );
  98.  
  99.    /* abnormal exit */
  100.    return( NULL );
  101.  
  102. }
  103.  
  104. /**/
  105.  
  106. /*
  107.  * ViewerWndFn( hWnd, wMessage, wParam, lParam ) : LONG
  108.  *
  109.  *    hWnd        window handle
  110.  *    wMessage    message number
  111.  *    wParam      additional message information
  112.  *    lParam      additional message information
  113.  *
  114.  * This window function processes all the messages related to the
  115.  * form viewer window.  With the menu a new form can be loaded, and
  116.  * existing form edited or printed.
  117.  *
  118.  */
  119.  
  120. LONG FAR PASCAL ViewerWndFn(
  121.    HWND        hWnd,
  122.    WORD        wMsg,
  123.    WORD        wParam,
  124.    LONG        lParam )
  125. {
  126.    /* local variables */
  127.    LONG        lResult;
  128.  
  129.    /* initialization */
  130.    lResult = FALSE;
  131.  
  132.    /* process each message */
  133.    switch( wMsg )
  134.       {
  135.   case WM_CREATE : /* window being created */
  136.      {
  137.         HWND     hWndEdit;
  138.         RECT     rcClient;
  139.  
  140.         /* retrieve client rectangle */
  141.         GetClientRect( hWnd, &rcClient );
  142.  
  143.         /* define property lists */
  144.         SetProp( hWnd, VIEWER_HWNDEDIT, NULL );
  145.         SetProp( hWnd, VIEWER_HFORMLIB, NULL );
  146.  
  147.         /* create edit child window */
  148.         hWndEdit = CreateWindow(
  149.               "Edit",
  150.               "",
  151.               WS_CHILD|WS_VISIBLE|WS_HSCROLL|WS_VSCROLL|
  152.                  ES_MULTILINE|ES_AUTOVSCROLL|ES_AUTOHSCROLL,
  153.               -1,
  154.               -1,
  155.               rcClient.right+2,
  156.               rcClient.bottom+2,
  157.               hWnd,
  158.               1,
  159.               INSTANCE(hWnd),
  160.               NULL
  161.            );
  162.  
  163.         /* continue if successful */
  164.         if ( !hWndEdit ) {
  165.            WARNING( hWnd, "Unable to create edit window!" );
  166.            PostMessage( hWnd, WM_CLOSE, NULL, (LONG)NULL );
  167.         } else
  168.            SetProp( hWnd, VIEWER_HWNDEDIT, hWndEdit );
  169.  
  170.      }
  171.      break;
  172.   case WM_SIZE : /* window being resized */
  173.  
  174.      /* resize edit window (if present) */
  175.      if ( GetProp(hWnd,VIEWER_HWNDEDIT) )
  176.         MoveWindow(
  177.            GetProp(hWnd,VIEWER_HWNDEDIT),
  178.            -1,
  179.            -1,
  180.            LOWORD(lParam)+2,
  181.            HIWORD(lParam)+2,
  182.            TRUE
  183.         );
  184.  
  185.      break;
  186.   case WM_INITMENU : /* initialize menu */
  187.  
  188.      /* enable edit & print menus */
  189.      if ( GetProp(hWnd,VIEWER_HFORMLIB) ) {
  190.         EnableMenuItem( wParam, IDM_EDIT, MF_ENABLED );
  191.         EnableMenuItem( wParam, IDM_PRINT, MF_ENABLED );
  192.      }
  193.  
  194.      break;
  195.   case WM_COMMAND : /* window command */
  196.  
  197.       /* process message */
  198.       switch( wParam )
  199.          {
  200.       case IDM_OPEN :
  201.         Dialog( hWnd, "ViewerOpen", OpenDlgFn );
  202.          break;
  203.       case IDM_EDIT :
  204.         {
  205.            HANDLE      hForm;
  206.            LPEDITFN    lpEditFn;
  207.  
  208.            /* retrieve edit data */
  209.  
  210.            /* call edit function dynamically */
  211.            hForm = GetProp( hWnd, VIEWER_HFORMLIB );
  212.            if ( hForm ) {
  213.               lpEditFn = GetProcAddress( hForm, FORM_EDIT );
  214.               (*lpEditFn)( hWnd, NULL );
  215.            }
  216.  
  217.         }
  218.          break;
  219.       case IDM_PRINT :
  220.         {
  221.            HANDLE      hForm;
  222.            LPPRINTFN   lpPrintFn;
  223.  
  224.            /* retrieve edit data */
  225.  
  226.            /* call print function dynamically */
  227.            hForm = GetProp( hWnd, VIEWER_HFORMLIB );
  228.            if ( hForm ) {
  229.               lpPrintFn = GetProcAddress( hForm, FORM_PRINT );
  230.               (*lpPrintFn)( hWnd, NULL );
  231.            }
  232.  
  233.         }
  234.          break;
  235.       case IDM_EXIT :
  236.          PostMessage( hWnd, WM_CLOSE, NULL, (LONG)NULL );
  237.          break;
  238.       case IDM_ABOUT :
  239.         Dialog( hWnd, "ViewerAbout", AboutDlgFn );
  240.          break;
  241.       }
  242.  
  243.       break;
  244.    case WM_DESTROY : /* destroy window */
  245.  
  246.      /* free form library (if present) */
  247.      if ( GetProp(hWnd,VIEWER_HFORMLIB) )
  248.         FreeLibrary( GetProp(hWnd,VIEWER_HFORMLIB) );
  249.  
  250.      /* remove property lists */
  251.      RemoveProp( hWnd, VIEWER_HWNDEDIT );
  252.      RemoveProp( hWnd, VIEWER_HFORMLIB );
  253.  
  254.      /* die gracefully */
  255.      PostQuitMessage( 0 );
  256.  
  257.      break;
  258.    default : /* send to default */
  259.       lResult = DefWindowProc( hWnd, wMsg, wParam, lParam );
  260.       break;
  261.    }
  262.  
  263.    /* return normal result */
  264.    return( lResult );
  265.  
  266. }
  267.  
  268. /**/
  269.  
  270. /*
  271.  * OpenDlgFn( hDlg, wMsg, wParam, lParam ) : BOOL ;
  272.  *
  273.  *    hDlg           handle to dialog box
  274.  *    wMsg          message or event
  275.  *    wParam         word portion of message
  276.  *    lParam         long portion of message
  277.  *
  278.  * This function is responsible for enabling the user to
  279.  * open a new form library.
  280.  *
  281.  */
  282.  
  283. BOOL FAR PASCAL OpenDlgFn(
  284.   HWND        hDlg,
  285.   WORD        wMsg,
  286.   WORD        wParam,
  287.   LONG        lParam )
  288. {
  289.   BOOL        bResult;
  290.  
  291.    /* warning level 3 compatibility */
  292.   lParam;
  293.  
  294.   /* initialization */
  295.   bResult = TRUE;
  296.  
  297.   /* process message */
  298.   switch( wMsg )
  299.      {
  300.   case WM_INITDIALOG :
  301.      CenterPopup( hDlg, GetParent(hDlg) );
  302.      break;
  303.   case WM_COMMAND :
  304.  
  305.      /* process sub-message */
  306.      switch( wParam )
  307.         {
  308.      case ID_FORM :
  309.  
  310.         /* enable or disable OK button */
  311.         if ( HIWORD(lParam) == EN_CHANGE )
  312.            EnableWindow(
  313.               GetDlgItem(hDlg,ID_OK),
  314.               SendMessage(
  315.                  GetDlgItem(hDlg,ID_FORM),
  316.                  WM_GETTEXTLENGTH,
  317.                  0,
  318.                  0L
  319.               ) ? TRUE : FALSE
  320.            );
  321.  
  322.         break;
  323.      case ID_OK :
  324.         {
  325.            HANDLE   hLib;
  326.            char     szLib[128];
  327.            char     szCaption[128];
  328.  
  329.            /* retrieve library name */
  330.            GetDlgItemText( hDlg, ID_FORM, szLib, sizeof(szLib) );
  331.  
  332.            /* attempt to load new library */
  333.            hLib = LoadLibrary( szLib );
  334.            if ( hLib >= 32 ) {
  335.  
  336.               /* free old form library */
  337.               if ( GetProp(PARENT(hDlg),VIEWER_HFORMLIB) )
  338.                  FreeLibrary(GetProp(PARENT(hDlg),VIEWER_HFORMLIB) );
  339.  
  340.               /* update main window caption */
  341.               lstrcpy( szCaption, "Form Viewer - " );
  342.               lstrcat( szCaption, szLib );
  343.  
  344.               SetWindowText( PARENT(hDlg), szCaption );
  345.  
  346.               /* save new library handle & close dialog box */
  347.               SetProp( PARENT(hDlg), VIEWER_HFORMLIB, hLib );
  348.               EndDialog( hDlg, TRUE );
  349.  
  350.            } else
  351.               WARNING( hDlg, "Unable to load library!" );
  352.  
  353.         }
  354.         break;
  355.      case ID_CANCEL :
  356.  
  357.         /* return false */
  358.         EndDialog( hDlg, FALSE );
  359.  
  360.         break;
  361.      default :
  362.         bResult = FALSE;
  363.         break;
  364.      }
  365.  
  366.      break;
  367.   default :
  368.      bResult = FALSE;
  369.      break;
  370.   }
  371.  
  372.   /* return final result */
  373.   return( bResult );
  374.  
  375. }
  376.  
  377. /**/
  378.  
  379. /*
  380.  * AboutDlgFn( hDlg, wMsg, wParam, lParam ) : BOOL ;
  381.  *
  382.  *    hDlg           handle to dialog box
  383.  *    wMsg          message or event
  384.  *    wParam         word portion of message
  385.  *    lParam         long portion of message
  386.  *
  387.  *   This function is responsible for processing all the messages
  388.  * that relate to the Viewer about dialog box.
  389.  *
  390.  */
  391.  
  392. BOOL FAR PASCAL AboutDlgFn(
  393.   HWND        hDlg,
  394.   WORD        wMsg,
  395.   WORD        wParam,
  396.   LONG        lParam )
  397. {
  398.   BOOL        bResult;
  399.  
  400.   /* warning level 3 compatibility */
  401.   lParam;
  402.  
  403.   /* initialization */
  404.   bResult = TRUE;
  405.  
  406.   /* process message */
  407.   switch( wMsg )
  408.      {
  409.   case WM_INITDIALOG :
  410.      CenterPopup( hDlg, GetParent(hDlg) );
  411.      break;
  412.   case WM_COMMAND :
  413.  
  414.      /* process sub-message */
  415.      if ( wParam == ID_OK )
  416.         EndDialog( hDlg, TRUE );
  417.      else
  418.         bResult = FALSE;
  419.  
  420.      break;
  421.   default :
  422.      bResult = FALSE;
  423.      break;
  424.   }
  425.  
  426.   /* return final result */
  427.   return( bResult );
  428.  
  429. }
  430.  
  431. /**/
  432.  
  433. /*
  434.  * Dialog( hParentWnd, lpszTemplate, lpfnDlgProc ) : BOOL
  435.  *
  436.  *      hParentWnd        handle to parent window
  437.  *      lpszTemplate      dialog box template
  438.  *      lpfnDlgProc       dialog window function
  439.  *
  440.  * This utility function displays the specified dialog box, using the
  441.  * template provided.  It automatically makes a new instance of the
  442.  * dialog box function.  Note that this function will NOT work
  443.  * correctly if an invalid or NULL parent window handle is provided.
  444.  *
  445.  */
  446.  
  447. BOOL FAR PASCAL Dialog( hParentWnd, lpszTemplate, lpfnDlgProc )
  448.   HWND        hParentWnd;
  449.   LPSTR       lpszTemplate;
  450.   FARPROC     lpfnDlgProc;
  451. {
  452.   /* local variables */
  453.   BOOL           bResult;
  454.   FARPROC        lpProc;
  455.  
  456.   /* display palette dialog box */
  457.   lpProc = MakeProcInstance( lpfnDlgProc, INSTANCE(hParentWnd) );
  458.   bResult = DialogBox( INSTANCE(hParentWnd), lpszTemplate,
  459.                        hParentWnd, lpProc );
  460.   FreeProcInstance( lpProc );
  461.  
  462.   /* return result */
  463.   return( bResult );
  464.  
  465. }
  466.  
  467. /**/
  468.  
  469. /*
  470.  * CenterPopup( hWnd, hParentWnd ) : BOOL
  471.  *
  472.  *      hWnd              window handle
  473.  *      hParentWnd        parent window handle
  474.  *
  475.  * This routine centers the popup window in the screen or display
  476.  * using the window handles provided.  The window is centered over
  477.  * the parent if the parent window is valid.  Special provision
  478.  * is made for the case when the popup would be centered outside
  479.  * the screen - in this case it is positioned at the appropriate
  480.  * border.
  481.  *
  482.  */
  483.  
  484. BOOL FAR PASCAL CenterPopup(
  485.      HWND     hWnd,
  486.      HWND     hParentWnd
  487.   )
  488. {
  489.   /* local variables */
  490.   int      xPopup;                 /* popup x position */
  491.   int      yPopup;                 /* popup y position */
  492.   int      cxPopup;                /* width of popup window */
  493.   int      cyPopup;                /* height of popup window */
  494.   int      cxScreen;               /* width of main display */
  495.   int      cyScreen;               /* height of main display */
  496.   int      cxParent;               /* width of parent window */
  497.   int      cyParent;               /* height of parent window */
  498.   RECT     rcWindow;               /* temporary window rect */
  499.  
  500.   /* retrieve main display dimensions */
  501.   cxScreen = GetSystemMetrics( SM_CXSCREEN );
  502.   cyScreen = GetSystemMetrics( SM_CYSCREEN );
  503.  
  504.   /* retrieve popup rectangle  */
  505.   GetWindowRect( hWnd, (LPRECT)&rcWindow );
  506.  
  507.   /* calculate popup extents */
  508.   cxPopup = rcWindow.right - rcWindow.left;
  509.   cyPopup = rcWindow.bottom - rcWindow.top;
  510.  
  511.   /* calculate bounding rectangle */
  512.   if ( hParentWnd ) {
  513.  
  514.      /* retrieve parent rectangle */
  515.      GetWindowRect( hParentWnd, (LPRECT)&rcWindow );
  516.  
  517.      /* calculate parent extents */
  518.      cxParent = rcWindow.right - rcWindow.left;
  519.      cyParent = rcWindow.bottom - rcWindow.top;
  520.  
  521.      /* center within parent window */
  522.      xPopup = rcWindow.left + ((cxParent - cxPopup)/2);
  523.      yPopup = rcWindow.top + ((cyParent - cyPopup)/2);
  524.  
  525.      /* adjust popup x-location for screen size */
  526.      if ( xPopup+cxPopup > cxScreen )
  527.         xPopup = cxScreen - cxPopup;
  528.  
  529.      /* adjust popup y-location for screen size */
  530.      if ( yPopup+cyPopup > cyScreen )
  531.         yPopup = cyScreen - cyPopup;
  532.  
  533.   } else {
  534.  
  535.      /* center within entire screen */
  536.      xPopup = (cxScreen - cxPopup) / 2;
  537.      yPopup = (cyScreen - cyPopup) / 2;
  538.  
  539.   }
  540.  
  541.   /* move window to new location & display */
  542.   MoveWindow(
  543.      hWnd,
  544.      ( xPopup > 0 ) ? xPopup : 0,
  545.      ( yPopup > 0 ) ? yPopup : 0,
  546.      cxPopup,
  547.      cyPopup,
  548.      TRUE
  549.   );
  550.  
  551.   /* normal return */
  552.   return( TRUE );
  553.  
  554. }
  555.